<template>
    <a-alert class="error mb16" v-if="errors.length" type="error" showIcon :message="`保存时发生错误：`">
        <template #description>
            <ul>
                <li v-for="(v, k) in errors" :key="k">{{ v }}</li>
            </ul>
        </template>
    </a-alert>
    <ProjTextEditor ref="textRef" :defaultBasis />

    <a-flex justify="space-between">
        <a-space>
            <a-button type="primary" @click="save" :loading><SaveOutlined />保存</a-button>
            <a-button @click="reset" :loading ><ReloadOutlined />还原所有修改</a-button>
        </a-space>
        <a-button type="primary" :loading>
            保存并前往下一页
            <ArrowRightOutlined />
        </a-button>
    </a-flex>
</template>

<script setup>
import { ref, inject } from 'vue'
import ProjTextEditor from '@proj/components/proj-text-editor.vue'
import axios from '@/axios'
import app from '@/app';

const errors = ref([]);
const textRef = ref();
const loading = ref(false);
const defaultBasis = ref("");
const data = inject("data");

function reset () {
    textRef.value.reset();
}

async function save () {
    const intro = textRef.value.intro;
    const basis = textRef.value.basis;

    let msg = await axios.post("/api/proj/common/SaveProjText", {
        id: data.basicInfo.ID,
        intro,
        basis
    });

    app.modals.showResponse(msg);
    if (msg.success) {
        Object.assign(data.extInfo, msg.extInfo);
    }
}

async function validate(){
    return await textRef.value.validate();
}
defineExpose({
    validate
});

</script>